/*
 * QSeparatorAdapter.cpp  0.4.0 / Nov 29, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "QSeparatorAdapter.h"

// TODO: add layout direction support

// QSeparatorAdapter :: Public Constructor. ------------------------------------

/*!
 * \brief Constructs new QLabelAdapter object.
 */
QSeparatorAdapter::QSeparatorAdapter()
  : QWidget(), m_orientation(Qt::Horizontal), m_useStylePalette(true)
{
  updateLabel();
}

// QSeparatorAdapter :: Public Member Functions. -------------------------------

/*!
 * \brief Returns separator orientation.
 *
 * \returns separator orientation
 */
Qt::Orientation QSeparatorAdapter::orientation() const
{
  return m_orientation;
}

/*!
 * \brief Sets separator orientation.
 *
 * \param orientation the orientation
 */
void QSeparatorAdapter::setOrientation(Qt::Orientation orientation)
{
  m_orientation = orientation;
}

/*!
 * \brief Returns separator text.
 *
 * \return separator text
 */
QString QSeparatorAdapter::text() const
{
  return m_text;
}

/*!
 * \brief Sets separator text.
 *
 * \param text the text
 */
void QSeparatorAdapter::setText(const QString text)
{
  m_text = text;
}

/*!
 * \brief Sets whether or not the separator uses style palette colors.
 *
 * \param useStylePalette \c true if use
 */
void QSeparatorAdapter::setUseStylePalette(bool useStylePalette)
{
  m_useStylePalette = useStylePalette;
}

// QSeparatorAdapter :: Protected Member Function. -----------------------------

/*!
 * \brief Updates separator label.
 */
void QSeparatorAdapter::updateLabel()
{
  if (!testAttribute(Qt::WA_SetFont)) {
    QFont labelFont = QApplication::font("QLabel");
    if (!font().isCopyOf(labelFont)) {
      setFont(labelFont);
      setAttribute(Qt::WA_SetFont, false);
    }
  }
}

// QSeparatorAdapter :: Overridden Member Functions. ---------------------------

/*!
 * \inheritDoc
 */
QSize QSeparatorAdapter::sizeHint() const
{
  QSize sh(DEFAULT_SEPARATOR_EXTENT, 4);
  if (!m_text.isEmpty()) {
    QFontMetrics fm = fontMetrics();
    QSize textSize = fm.size(Qt::TextSingleLine, m_text);
    sh.setWidth(sh.width() + textSize.width());
    if (sh.height() < textSize.height()) {
      sh.setHeight(textSize.height());
    }
  }
  if (m_orientation == Qt::Vertical) {
    sh.transpose();
  }
  return sh;
}

/*!
 * \inheritDoc
 */
void QSeparatorAdapter::changeEvent(QEvent *e)
{
  if (e->type() == QEvent::PaletteChange) {
    updateLabel();
  }
}

/*!
 * \inheritDoc
 */
void QSeparatorAdapter::paintEvent(QPaintEvent *e)
{
  Q_UNUSED(e); // TODO: use event rect
  int x1 = 0;
  int y1 = 0;
  int x2 = 0;
  int y2 = 0;
  bool enabled = isEnabled();
  bool horizontal = m_orientation == Qt::Horizontal;
  QPainter painter(this);
  QRect r = rect();
  if (!m_text.isEmpty()) {
    QPalette palette = this->palette();
    if (m_useStylePalette) {
      QStyleOptionGroupBox options;
      options.initFrom(this);
      QColor labelColor(style()->styleHint(QStyle::SH_GroupBox_TextLabelColor,
                                           &options, this));
      palette = options.palette;
      if (enabled) {
        palette.setColor(QPalette::Inactive, QPalette::Foreground, labelColor);
        palette.setColor(QPalette::Active, QPalette::Foreground, labelColor);
      } else {
        palette.setColor(QPalette::Disabled, QPalette::Foreground, labelColor);
      }
    }
    if (horizontal) {
      style()->drawItemText(&painter, r, Qt::AlignVCenter, palette, enabled,
                            m_text, foregroundRole());
      x1 = 4;
    } else {
      painter.save();
      painter.translate(r.width() + 1, 0);
      painter.rotate(90);
      style()->drawItemText(&painter, r, Qt::AlignVCenter, palette, enabled,
                            m_text, foregroundRole());
      painter.restore();
      y1 = 4;
    }
  }
  if (horizontal) {
    x1 = x1 + r.x() + fontMetrics().width(m_text);
    y1 = (r.height() >> 1);
    x2 = r.x() + r.width();
    y2 = y1;
  } else {
    x1 = r.x() + (r.width() >> 1);
    y1 = y1 + fontMetrics().width(m_text);
    x2 = x1;
    y2 = r.height();
  }
  qDrawShadeLine(&painter, x1, y1, x2, y2, palette(), true, 1, 0);
}

// QSeparatorAdapter :: Public Static Constant Data Member. --------------------

const int QSeparatorAdapter::DEFAULT_SEPARATOR_EXTENT = 30;
